package com.base.vistter.iframe.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.base.vistter.iframe.entity.SysTeam;
import com.base.vistter.iframe.entity.SysTeamUser;
import com.base.vistter.iframe.entity.SysUser;
import com.base.vistter.iframe.exception.IframeException;
import com.base.vistter.iframe.mapper.SysTeamUserMapper;
import com.base.vistter.iframe.mapper.SysUserMapper;
import jakarta.annotation.Resource;
import kotlin.collections.ArrayDeque;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Transactional
@Slf4j
public class SysTeamUserService extends BaseService<SysTeamUserMapper, SysTeamUser> {

    @Resource(name = "sysUserMapper")
    private SysUserMapper sysUserMapper;
    @Transactional(readOnly = true)
    public Page<SysUser> unjoin(Page<SysUser> page, String username, String phone, Integer teamId) throws IframeException{
        LambdaQueryWrapper<SysUser> lambdaQueryWrapper = new LambdaQueryWrapper<SysUser>();
        if(StringUtils.isNotEmpty(username)){
            lambdaQueryWrapper.eq(SysUser::getUsername, username);
        }
        if(StringUtils.isNotEmpty(phone)){
            lambdaQueryWrapper.eq(SysUser::getPhone, phone);
        }
        List<SysTeamUser> list = this.getSysTeamUserByTeamId(teamId);
        if(list != null && !list.isEmpty()){
            List<Integer> userIds = list.stream().map(SysTeamUser::getUserId).collect(Collectors.toList());
            lambdaQueryWrapper.notIn(SysUser::getId , userIds);
        }
        return this.sysUserMapper.selectPage(page , lambdaQueryWrapper);
    }
    @Transactional(readOnly = true)
    public Page<Map<String,Object>> join(Page<Map<String,Object>> page, String username, String phone, Integer teamId) throws IframeException{
        long count = this.baseMapper.selectUserCount(teamId, username, phone);
        page.setTotal(count);
        if(count == 0l){
            page.setTotal(count);
            return page;
        }
        long start = (page.getCurrent() - 1) * page.getSize();
        long size = page.getSize();
        List<Map<String,Object>> result = this.baseMapper.selectUserList(teamId ,username, phone, start, size);
        page.setRecords(result);
        return page;
    }

    public List<SysTeamUser> getSysTeamUserByTeamId(Integer teamId) throws IframeException{
        LambdaQueryWrapper<SysTeamUser> lambdaQueryWrapper = new LambdaQueryWrapper<SysTeamUser>();
        lambdaQueryWrapper.eq(SysTeamUser::getTeamId , teamId);
        return super.selectList(lambdaQueryWrapper);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public Boolean saveTeamUser(SysTeam sysTeam) throws IframeException{
        if(sysTeam.getUserIds() != null && !sysTeam.getUserIds().isEmpty()){
            List<SysTeamUser> list = new ArrayDeque<SysTeamUser>();
            for(int i = 0 ; i < sysTeam.getUserIds().size(); i++){
                SysTeamUser sysTeamUser = new SysTeamUser(sysTeam.getId() , sysTeam.getUserIds().get(i));
                list.add(sysTeamUser);
            }
            return super.saveBatch(list);
        }
        return true;
    }
}
